package com.chuangke.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.dto.UserDeptRequest;
import com.chuangke.admin.service.SysUserDataDeptService;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.utils.SecurityUtils;

@RestController
@RequestMapping("userdept")
public class SysUserDataDeptController {

	@Autowired
	private SysUserDataDeptService sysUserDataDeptService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody UserDeptRequest userDeptRequest) {
		sysUserDataDeptService.save(userDeptRequest);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<String> ids) {
		sysUserDataDeptService.removeByIds(ids) ;
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findall")
	public HttpResult findAll() {
		return HttpResult.ok(sysUserDataDeptService.findAll());
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/find/{userId}")
	public HttpResult find(@PathVariable("userId") String userId) {
		return HttpResult.ok(sysUserDataDeptService.find(userId));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findown")
	public HttpResult find() {
		return HttpResult.ok(sysUserDataDeptService.find(SecurityUtils.getUserId()));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findUserDeptWithChildren/{userId}")
	public HttpResult findUserDeptWithChildren(@PathVariable("userId") String userId) {
		return HttpResult.ok(sysUserDataDeptService.findUserDeptWithChildren(userId));
	}
}
